package com.adtec.base.cloud.baseservice.rbac.system.controller;


import com.adtec.base.cloud.baseservice.rbac.config.DbLoadSysConfig;
import com.adtec.base.cloud.baseservice.rbac.system.service.SysConfigService;
import com.adtec.base.cloud.commons.exception.AjaxResponse;
import com.adtec.base.cloud.persistence.system.model.SysConfig;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/sysconfig")
@Tag(name = "sysconfig", description = "参数配置")
public class SysConfigController {

    @Resource
    private DbLoadSysConfig dbLoadSysConfig;

    @PostMapping(value = "/all")
    @Operation(description = "获取所有参数配置项")
    public List<SysConfig> all() {
        return dbLoadSysConfig.getSysConfigList();
    }

    @PostMapping(value = "/refresh")
    @Operation(description = "获取所有参数配置项(集群无法保证配置在内存的刷新,策略已修改)")
    public List<SysConfig> refresh() {
        return dbLoadSysConfig.getSysConfigList();
    }


    @Resource
    private SysConfigService sysconfigService;

    @PostMapping(value = "/query")
    @Operation(description = "参数配置：查询")
    @Parameters({
            @Parameter(name = "configLike", description = "参数配置(模糊查询)"),
    })
    public List<SysConfig> query(@RequestParam(name = "configLike", required = false) String configLike) {
        return sysconfigService.queryConfigs(configLike);
    }

    @PostMapping(value = "/update")
    @Operation(description = "参数配置：更新")
    public AjaxResponse update(@RequestBody SysConfig sysConfig) {
        sysconfigService.updateConfig(sysConfig);
        return AjaxResponse.success("更新配置成功！");
    }

    @PostMapping(value = "/add")
    @Operation(description = "参数配置：新增")
    public AjaxResponse add(@RequestBody SysConfig sysConfig) {
        sysconfigService.addConfig(sysConfig);
        return AjaxResponse.success("新增配置成功！");
    }

    @PostMapping(value = "/delete")
    @Operation(description = "参数配置：删除")
    @Parameters({
            @Parameter(name = "configId", description = "配置ID"),
    })
    public AjaxResponse delete(@RequestParam Long configId) {
        sysconfigService.deleteConfig(configId);
        return AjaxResponse.success("删除配置成功!");
    }

}
